var util = require('../../utils/util.js');
var api = require('../../config/api.js');

var app = getApp();
Page({
  data: {
    navList: [], 
    idx:0,
    categoryList: [], 
    currentCategory: {},
    scrollLeft: 0,
    scrollTop: 0,
    goodsCount: 0,
    scrollHeight: 0,
    channelid:0
  },
  onLoad: function (options) {  
    var that = this;
    this.getCatalog();   
    var  id = app.globalData.channelid;  
    if (id != null){    
      this.getCurrentCategory(id); 
    } 
  },
  getCatalog: function () {  
    let that = this;
    // wx.showLoading({
    //   title: '加载中...',
    // });
    //一级类目
    util.request(api.CatalogList).then(function (res) { 
        //that.channelid =parseInt(res.data.currentCategory.id);
        that.setData({
          navList: res.data.categoryList, 
        });   
        
        wx.hideLoading(); 
   });

    //搜索统计
    util.request(api.GoodsCount).then(function (res) {
      that.setData({
        goodsCount: res.data.goodsCount
      });
    }); 
  },
  //分类目录当前分类数据接口/显示的子目录
  getCurrentCategory: function (id) { 
    let that = this; 
    util.request(api.CatalogCurrent, { id: id }) 
      .then(function (res) { 
        that.setData({
           currentCategory: res.data.currentCategory,
          categoryList: res.data.currentSubCategory
        });
      });
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },  
  getList: function () { 
    var that = this; 
    util.request(api.ApiRootUrl + '/category/current' + that.data.currentCategory.id)
      .then(function (res) {
        that.setData({
          categoryList: res.data.currentSubCategory,
        });
      });
  },
  //切换数据
  switchCate: function (event) { 
    var that = this;
    var currentTarget = event.currentTarget; 
    if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
      return false;
    } 
    this.getCurrentCategory(event.currentTarget.dataset.id);
  }
})